//
//  VisitorLoginView.swift
//  Weibo
//
//  Created by 张小东 on 16/1/11.
//  Copyright © 2016年 Mr.Zhang. All rights reserved.
//

import UIKit
// 导入SnapKit框架
import SnapKit

@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    // MARK 协议方法
    
    // 用户将要注册
    func userWillRegister()
    // 用户将要登录
    func userWillLogin()
    
}

class VisitorLoginView: UIView {
    //声明代理
    
    weak var visitorViewDelegate: VisitorLoginViewDelegate?
    // MARK 实现按钮的监听事件方法
    @objc private func loginBtnDidClick() {
        //使用代理调用方法
        visitorViewDelegate?.userWillLogin()
    }
    @objc private func registerBtnDidClick() {
        visitorViewDelegate?.userWillRegister()
    }
    
    
    // 外界调用的重要方法
    func setupUIWithInfo(imageName: String? , tipText: String) {
        if let img = imageName {
            circleIcon.image = UIImage(named: img)
            // 隐藏大房子
            lagerHouse.hidden = true
            bringSubviewToFront(circleIcon)
            
        }else {
            // 说明在此页
            startAnimation()
            
        }
        tipLabel.text = tipText
        
    }
    
    // 设置 圈圈 动画效果
    func startAnimation() {
        // 基础动画
        let base = CABasicAnimation(keyPath: "transform.rotation")
        
        // 设置重复次数
        base.repeatCount = MAXFLOAT
        base.toValue = 2 * M_PI
        // 持续时间
        base.duration = 10
        // 设置 动画完成 / 页面时区活跃状态  不移除图层
        base.removedOnCompletion = false
        // 添加动画
        circleIcon.layer.addAnimation(base, forKey: nil)
        
        
    }
    
    
    
    // 重写init 设置frame
    override init(frame: CGRect) {
        super.init(frame : frame)
        setUpUI()
    }
    
    // 实现了默认报错
    // 程序员如果通过 xib / sb 加载该视图 就会崩溃
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        addSubview(circleIcon)
        addSubview(backView)
        addSubview(lagerHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        // 添加约束  链式约束
        
        //    snp_makeConstraints  表示一条链式约束
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            // 垂直居中 添加偏移量
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        // 给大房子设置约束
        lagerHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        // 给提示文字设置约束
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon)
            //        make.centerY.equalTo(self.snp_centerY).offset(20)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            //        let w : Int =  self.snp_width
            make.height.equalTo(40)
            make.width.equalTo(225)
        }
        // 给登陆按钮设置约束
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        backView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(registerBtn.snp_top)
        }
        
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        // 给按钮注册点击事件
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        
    }
    //懒加载所有子视图
    
    // 背景图片
    lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // 圆圈
    lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 登陆界面的大房子
    lazy var lagerHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    // 提示文字
    lazy var tipLabel : UILabel = UILabel.labelFactory(16, title: "登陆后,你将可以随意查看别人的微博,或者发送自己的状态等", color: UIColor.darkGrayColor())
//    
//    {
//        let label = UILabel()
//        label.font = UIFont.systemFontOfSize(16)
//        label.text = "登陆后,你将可以随意查看别人的微博,或者发送自己的状态等"
//        label.textColor = UIColor.darkGrayColor()
//        label.textAlignment = .Center
//        label.numberOfLines = 0;
//        label.sizeToFit()
//        return label
//    }()
    // 登陆按钮
    lazy var loginBtn: UIButton = UIButton(title: "登  录" , fontSize: 17, color:UIColor.darkGrayColor(), backImageName: "common_button_white_disable")
//    {
//        let btn = UIButton()
//        btn.setTitle("登  录", forState: .Normal)
//        btn.setTitleColor(UIColor.brownColor(), forState: .Normal)
//        //        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(17)
//        let image = UIImage(named: "common_button_white_disable")!
//        let w = Int(image.size.width * 0.5)
//        let h = Int(image.size.height * 0.5)
//        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
//        return btn
//    }()
    
    // 注册按钮
    lazy var registerBtn : UIButton = UIButton(title: "注  册", fontSize: 17, color: UIColor.orangeColor(), backImageName: "common_button_white_disable")
//    {
//        let btn = UIButton()
//        //        btn.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
//        btn.setTitle("注  册", forState: .Normal)
//        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(17)
//        let image = UIImage(named: "common_button_white_disable")!
//        let w = Int(image.size.width * 0.5)
//        let h = Int(image.size.height * 0.5)
//        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
//        return btn
//    }()
    
}
